package com.frxuexi.report.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.UiState
import com.frxuexi.report.ReportIntent
import com.frxuexi.report.ReportViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Composable
fun ReportPage(navHostController: NavHostController, json: String,vm:ReportViewModel= hiltViewModel()) {
    val data = Gson().fromJson(json, ReportData::class.java)
    Log.i("===", data.toString())
    val time = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
    val week = TimeUtils.getChineseWeek(System.currentTimeMillis())

    var today by remember {
        mutableStateOf("")
    }

    var tomorrow by remember {
        mutableStateOf("")
    }

    var nowTitle= if (data.type==1) "今日" else if(data.type==2) "本周" else "本月"
    var nextTitle= if (data.type==1) "明日" else if(data.type==2) "下周" else "下月"

    var isShowCom =if(arrayOf(4,5,6).contains(data.type)) false else true

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        ToastUtils.showLong("提交成功")
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })

    Column() {
        BaseTitle(
            title = "$time $week (今日)",
            navHostController = navHostController,
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    Color.White
                )
        )
        Column(
            Modifier
                .fillMaxSize()
                .background(colorResource(id = R.color.background))
                .padding(20.dp, 10.dp)
        ) {
            if(isShowCom) {
                Text(
                    text = "${nowTitle}工作",
                    Modifier
                        .fillMaxWidth()
                        .border(1.dp, Color.LightGray, RoundedCornerShape(3.dp))
                        .background(Color.White, RoundedCornerShape(topStart = 3.dp, topEnd = 3.dp))
                        .padding(20.dp, 15.dp)

                )
                TextField(
                    value = today, onValueChange = { today = it },
                    label = { Text(text = "请输入${nowTitle}工作内容") },
                    colors = TextFieldDefaults.colors(
                        focusedContainerColor = Color.White,
                        unfocusedContainerColor = Color.White,
                        focusedIndicatorColor = Color.White,
                        unfocusedIndicatorColor = Color.White
                    ),

                    modifier = Modifier
                        .fillMaxWidth()
                        .background(Color.White)
                        .height(80.dp)
                        .padding(horizontal = 10.dp)
                )
                Spacer(modifier = Modifier.height(10.dp))
                Text(
                    text = "${nextTitle}计划",
                    Modifier
                        .fillMaxWidth()
                        .border(1.dp, Color.LightGray, RoundedCornerShape(3.dp))
                        .background(Color.White, RoundedCornerShape(topStart = 3.dp, topEnd = 3.dp))
                        .padding(20.dp, 15.dp)

                )
            }
            val text=if(isShowCom) "${nextTitle}工作计划" else data.title
            TextField(
                value = tomorrow, onValueChange = { tomorrow = it },
                label = { Text(text = "请输入$text") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor=Color.White,
                    unfocusedContainerColor = Color.White,
                    focusedIndicatorColor=Color.White,
                    unfocusedIndicatorColor = Color.White
                ),

                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.White)
                    .height(80.dp)
                    .padding(horizontal = 10.dp)
            )
            Spacer(modifier = Modifier.height(10.dp))
            Button(onClick = {
                val map = mutableMapOf<String, Any>()
                map["reportType"]=data.type
                map["reportTime"]=System.currentTimeMillis()
                map["reportRemind"]="王先生"
                map["reportNowTitle"]="${nowTitle}工作"
                map["reportNowValue"]=today
                map["reportNextTitle"]="${nextTitle}计划"
                map["reportNextValue"]=tomorrow
                vm.sendIntent(ReportIntent.addReport(map))
            },Modifier.fillMaxWidth()) {
                Text(text = "提交")
            }
        }
    }
}